package com.crayfish.quduo.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseMvpActivity;
import com.crayfish.quduo.commom.Params;
import com.crayfish.quduo.http.RetrofitHelper;
import com.crayfish.quduo.model.JokeItemBean;
import com.crayfish.quduo.presenter.JokePresenter;
import com.crayfish.quduo.presenter.contract.JokeContract;
import com.crayfish.quduo.ui.adapter.EndlessRecyclerOnScrollListener;
import com.crayfish.quduo.ui.adapter.FooterHolder;
import com.crayfish.quduo.ui.adapter.JokeListAdapter;
import com.crayfish.quduo.utils.ToastUtil;
import com.crayfish.quduo.widget.RecycleViewDivider;

import java.util.List;

import butterknife.BindView;

/**
 * 作者：crayfish(徐杰)
 * 描述：
 * Created by crayfish on 2017/3/28.
 */
public class JokeListActivity extends BaseMvpActivity<JokePresenter> implements JokeContract.View{

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    JokeListAdapter jokeListAdapter;

    private int currentPage = 1;
    private int mState = FooterHolder.Normal;
    private Boolean isRefresh = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list;
    }

    @Override
    protected void initEventAndData() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        swipeRefreshLayout.setColorSchemeColors(Color.BLUE,Color.GREEN,Color.YELLOW,Color.RED);
        swipeRefreshLayout.setDistanceToTriggerSync(300);
        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //检查是否处于刷新状态
                if (!isRefresh) {
                    isRefresh = true;
                    //模拟加载网络数据，这里设置4秒，正好能看到4色进度条
                    new Handler().postDelayed(new Runnable() {
                        public void run() {

                            //显示或隐藏刷新进度条
                            swipeRefreshLayout.setRefreshing(false);
                            //修改adapter的数据
                            mPresenter.getJokeData(currentPage=1);
                            isRefresh = false;
                        }
                    }, 3000);
                }
            }
        });
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new RecycleViewDivider(this,LinearLayoutManager.VERTICAL));
        recyclerView.setHasFixedSize(true);//固定高度提高性能
        jokeListAdapter = new JokeListAdapter(this);
        jokeListAdapter.setOnRecyclerViewItemClickListener(new JokeListAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, JokeItemBean.Item item) {
                Intent intent = new Intent(JokeListActivity.this, ImageActivity.class);
                intent.putExtra(Params.JOKE_ITEM,item);
                startActivity(intent);
            }
        });
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(){
            @Override
            public void onLoadNextPage(View view) {
                super.onLoadNextPage(view);
                if (mState == FooterHolder.Loading) {
                    return;
                }
                mPresenter.getJokeData(++currentPage);
            }
        });
        recyclerView.setAdapter(jokeListAdapter);//替换Adapter
        mPresenter.getJokeData(currentPage);
    }

    protected void setState(int mState) {
        this.mState = mState;
        ((JokeListActivity) this).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                changeAdapterState();
            }
        });
    }

    //改变底部bottom的样式
    protected void changeAdapterState() {
        if (jokeListAdapter != null && jokeListAdapter.footerHolder != null) {
            jokeListAdapter.footerHolder.setStatus(mState);
        }
    }

    @Override
    protected JokePresenter createPresenter() {
        return new JokePresenter(new RetrofitHelper());
    }

    @Override
    public void showList(List<JokeItemBean.Item> jokeItemBean) {
        if(jokeItemBean.size() > 0){
            if(currentPage == 1) {
                jokeListAdapter.clear();
                jokeListAdapter.addAll(jokeItemBean);
            }else{
                jokeListAdapter.addAll(jokeItemBean);
            }
        }else{
            setState(FooterHolder.TheEnd);
        }
    }

    @Override
    public void showError(String msg) {
        setState(FooterHolder.NetWorkError);
        ToastUtil.show(msg);
    }
}
